package tpenchere.controllers;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import tpenchere.model.User;
import tpenchere.service.EnchereServiceOperations;
import tpenchere.ui.AbstractView;
import tpenchere.ui.contents.RegisterWindow;

import org.jdesktop.application.Action;

public class RegisterController extends DefaultController {

	/** the view is useful to retrieve info (e.g. the selected item) */
	private final RegisterWindow view;

	public RegisterController(RegisterWindow pView) {
		view = pView;
	}
	
	@Action(name = "view.register.button.register")
	public void createUser() {
		User newUser = null;
		String login = view.getLogin().getText();
		EnchereServiceOperations serviceImpl = provider.getService();
		
		if(!serviceImpl.loginUsed(login)) {
			newUser = new User();
			newUser.pseudo = login;
			newUser.nom = view.getLastname().getText();
			newUser.adresse = view.getAddress().getText();
			newUser.idUser = 0;
			newUser.motDePasse = "";
			newUser = serviceImpl.createUser(newUser);
			
			view.showMessage(resourceMap.getString("view.register.dialog.registration.complete", newUser.motDePasse),
					resourceMap.getString("view.register.dialog.registration.title"));
			this.switchToView(AbstractView.ViewType.LOGIN);
		} else {
			view.showError(resourceMap.getString("view.register.dialog.registration.already.exists", login),
					resourceMap.getString("view.register.dialog.registration.title"));
		}
	}
}
